<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>活动列表</title>
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/common.css" media="all">
    <script src="/js/layui/layui.all.js"></script>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div style="text-align: center;line-height: normal">
                <div class="layui-inline">
                    <input class="layui-input" name="id" id="test-table-demoReload" placeholder="请输入标题" autocomplete="off">
                </div>

                <div class="layui-inline">
                    <div class="layui-form">
                        <div class="layui-form-item">
                            <label class="layui-form-label" >状态</label>
                            <div class="layui-input-inline">
                                <select id="status" name="status">
                                    <option value="-1" >不限</option>
                                    <option value="0" >禁用</option>
                                    <option value="1" >启用</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin search" lay-submit lay-filter="LAY-user-back-search" >
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>搜索
                    </button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <table class="layui-hide" id="t" lay-filter="t"></table>
        </div>
    </div>
</div>

</body>
<script type="text/html" id="caozuo">
    {{#  if (d.status == 1) {  }}
    <input type="button" value="禁用" class="layui-btn layui-btn-sm" lay-event="jinyong"/>
    {{# } else if(d.status == 0){  }}
    <input type="button" value="启用" class="layui-btn layui-btn-sm" lay-event="qiyong"/>
    {{# } }}
    {{#  if (d.xianshi == 1) {  }}
    <input type="button" value="隐藏" class="layui-btn layui-btn-sm" lay-event="yincang"/>
    {{# } else if(d.xianshi == 0){  }}
    <input type="button" value="显示" class="layui-btn layui-btn-sm" lay-event="xianshi"/>
    {{# } }}
    <input type="button" value="编辑" class="layui-btn layui-btn-sm" lay-event="detail"/>
    <input type="button" value="删除" class="layui-btn layui-btn-sm" lay-event="delete"/>
</script>
<script>
    var $ = layui.$;
    var table = layui.table;
    var form = layui.form;
    var language=navigator.language;//zh-CN简体中文  zh中文 zh-TW中文繁体  en英语  en-US英语美国 th泰语

    form.render('select');
    table.render({
        elem: '#t',
        id: 'tt',
        toolbar: '#tb',
        url: '/activities/selectActivities',
        cols: [[
                {fixed: 'left',field: '',title: '序号',width: 70,type:'numbers',align: 'center'},
                {field: 'title',title: '标题',width: 200,align: 'center'},
                {field: 'date1',title: '开始时间',width: 160,align: 'center',},
                {field: 'date2',title: '结束时间',width: 160,align: 'center'},
                {field: 'url',title: '网址链接',width: 200,align: 'center'},
                {fixed: 'right',field:'status',align : 'center', title:'状态',width:120, templet: function (d) {
                        return d.status == 0 ? "<span style='color: red'>已禁用</span>" :  "<span style='color: green'>启用中</span>"
                    }},
                {fixed: 'right',field:'xianshi',align : 'center', title:'是否显示',width:120, templet: function (d) {
                        return d.xianshi == 0 ? "<span style='color: red'>隐藏</span>" :  "<span style='color: green'>显示</span>"
                    }},
                {fixed: 'right',toolbar: '#caozuo',title: '操作',width: 250,align: 'center'}
            ]],
        page: true,
        page: {
            curr: layui.data("thaitrade_activities_page").index
        },
        done: (res, curr, count) => {
            layui.data("thaitrade_activities_page", {
                key: 'index',
                value: curr
            });
        }
    });

    function search(){
        var text = $('#test-table-demoReload').val();
        table.reload('tt',{
            url:'/activities/selectActivities',
            where:{
                title:text,
                status: $("#status").val(),
            },
            page:{
                curr:1
            }
        })
    }

    //搜索
    $('.search').on('click',function () {
        search();
    });

    // 关键词搜索框添加绑定回车函数
    $('#test-table-demoReload').bind('keypress', function(event) {
        search();
    });

    //监听工具条
    table.on('tool(t)', function (obj) {
        var data = obj.data;
        if (obj.event === 'detail') {
            layer.open({
                type: 2,
                area: ['100%', '100%'],
                content: 'updateActivities.html?id='+data.id, //这里content是一个普通的String
                end: function(){
                    location.reload();
                }
            });
        }else if (obj.event === 'jinyong'){
            layer.confirm("确定要禁用?",["确定","取消"],function(){
                $.ajax({
                    url:'/activities/updateStatus',
                    data:{id:data.id,status:0},
                    success:function(data){
                        if (data.code==200)
                        {
                            layer.msg("禁用成功",{icon:1,time:1000},function(){
                                location.reload()
                            });
                        }else{
                            layer.msg(data.msg,{icon:1,time:1000});
                        }
                    }
                })
            });
        }else if (obj.event === 'qiyong'){
            layer.confirm("确定要启用?",["确定","取消"],function(){
                $.ajax({
                    url:'/activities/updateStatus',
                    data:{id:data.id,status:1},
                    success:function(data){
                        if (data.code==200)
                        {
                            layer.msg("启用成功",{icon:1,time:1000},function(){
                                location.reload()
                            });
                        }else{
                            layer.msg(data.msg,{icon:1,time:1000});
                        }
                    }
                })
            });
        }else if (obj.event === 'yincang'){
            layer.confirm("确定要隐藏?",["确定","取消"],function(){
                $.ajax({
                    url:'/activities/updateXianshi',
                    data:{id:data.id,xianshi:0},
                    success:function(data){
                        if (data.code==200)
                        {
                            layer.msg("隐藏成功",{icon:1,time:1000},function(){
                                location.reload()
                            });
                        }else{
                            layer.msg(data.msg,{icon:1,time:1000});
                        }
                    }
                })
            });
        }else if (obj.event === 'xianshi'){
            layer.confirm("确定要显示?",["确定","取消"],function(){
                $.ajax({
                    url:'/activities/updateXianshi',
                    data:{id:data.id,xianshi:1},
                    success:function(data){
                        if (data.code==200)
                        {
                            layer.msg("显示成功",{icon:1,time:1000},function(){
                                location.reload()
                            });
                        }else{
                            layer.msg(data.msg,{icon:1,time:1000});
                        }
                    }
                })
            });
        }else if (obj.event === "delete"){
            layer.confirm("确定要删除该活动?",["确定","取消"],function(){
                $.ajax({
                    url:'/activities/delete',
                    data:{id:data.id},
                    success:function(data){
                        if (data.code==200)
                        {
                            layer.msg("删除成功",{icon:1,time:1000},function(){
                                search();
                            });
                        }else{
                            layer.msg(data.msg,{icon:1,time:1500});
                        }
                    }
                })
            });
        }
    });

    //监听启用操作
    form.on('checkbox(lockDemo)', function(obj){
        var id = this.value;
        var status = obj.elem.checked;
        var zhuangtai;
        if(status){
            zhuangtai=1;
        }else{
            zhuangtai=0;
        }
        $.ajax({
            url:"/activities/updateStatus"
            ,type:"get"
            ,data:{id:id,status:zhuangtai}
            ,contentType: 'application/json'
            ,success:function (data) {
                if(data.code==200){
                    layer.close(layer.index);
                    layer.msg('操作成功');
                }else if(data.code==500){
                    obj.elem.checked=!status;
                    layui.form.render();
                    layer.msg('操作失败');
                }else{
                    obj.elem.checked=!status;
                    layui.form.render();
                    layer.msg('没有权限');
                }
            }
            ,error:function () {
                obj.elem.checked=!status;
                layui.form.render();
                layer.msg("服务器接口异常");
            }
        });
    });


</script>
</html>